// 页面路径：store/index.js 
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex); //vue的插件机制

//Vuex.Store 构造器选项
const store = new Vuex.Store({
	state: { //存放状态
		userinfo: uni.getStorageSync('userinfo') ? JSON.parse(uni.getStorageSync('userinfo')) : '',
		usertoken: uni.getStorageSync('usertoken') ? uni.getStorageSync('usertoken') : '',
		location: uni.getStorageSync('location') ? JSON.parse(uni.getStorageSync('location')) : '',
		userid: uni.getStorageSync('userid') ? uni.getStorageSync('userid') : '',
		usercart: [],//用户购物车
		cartid:0,//购物车id
		totalprice: 0,//购物车总额
		loginshow:false,//有没有登录
		coupondata:'',//优惠卷信息
		addresdata:'',//地址信息
		kfphoneNumber:'',//客服电话
		playvideo:'',//当前点击的视频
		tableNo: 0//餐桌号
	},
	getters: {
		getuserinfo(state) {
			return state.userinfo
		},
		getuserid() {
			return state.userid
		},
		gettoken(state) {
			return state.usertoken
		},
		getlocation(state) {
			return state.location
		},

		gettotalprice(state) {
			return state.shop.totalprice
		},
		getusercart(state) {
			return state.usercart
		},
		getusercart(state) {
			return state.cartid
		},
		getguests(state) {
			return state.guests
		},
		getTableNo(state) {
			return state.tableNo
		},
		
		
	},
	mutations: {

		/******用户信息*********/
		setUserinfo(state, e) {
			uni.setStorageSync('userinfo', JSON.stringify(e))
			state.userinfo = e;
		},
		// 更新用户信息
		updateUserInfo(state, e) {
			const newUserInfo = { ...state.userinfo, ...e };
			uni.setStorageSync('userinfo', JSON.stringify(newUserInfo));
			state.userinfo = newUserInfo;
		},
		// 更新用户积分
		updateUserPoints(state, points) {
			const newUserInfo = { ...state.userinfo, points: points };
			uni.setStorageSync('userinfo', JSON.stringify(newUserInfo));
			state.userinfo = newUserInfo;
		},
		// 清除用户信息
		clearUserInfo(state) {
			uni.removeStorageSync('userinfo');
			state.userinfo = '';
		},
		//token
		setUsertoken(state, e) {
			uni.setStorageSync('usertoken', e)
			state.usertoken = e;
		},
		//用户id
		setUserid(state, e) {
			uni.setStorageSync('userid', e)
			state.userid = e;
		},
		//登录状态
		setLoginshow(state, e) {
			state.loginshow = e;
		},
		//购物车id
		setCartid(state, e) {
			state.cartid = e;
		},
		//优惠卷数据
		setCoupon(state, e) {
			state.coupondata = e;
		},
		//用户地址信息
		setAddress(state, e) {
			state.addresdata = e;
		},
		setUserCart(state, e) {
			state.usercart = e;
			let sumprice = 0;
			e.map(item => sumprice += (item.productPrice*item.count));//计算总价
			state.totalprice=sumprice
		},
		setTotalprice(state, prams) {
			let newDatalist = state.usercart.map(_item => {
				if (_item.id == prams.item.id) {
					if (prams.symbol == '+') {
						_item.count++;
						state.totalprice += prams.item.productPrice;
					} else {
						_item.count -= 1;
						state.totalprice -= prams.item.productPrice;
					}
				}
				return _item;
			})
			state.usercart = newDatalist;
		},
		clearCart(state) {
			state.usercart= [];
			state.totalprice = 0;
		},
		//客服电话
		setKfphoneNumber(state, e) {
			state.kfphoneNumber = e;
		},
		setplayvideo(state, e) {
			state.playvideo = e;
		},
		//设置餐桌号
		setTableNo(state, e) {
			state.tableNo = e;
		},
		

	}
})
export default store